<?php

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$user_id    = get_loggedin_userid();
$limit      = $perpage = get_input('wperpage');
$page       = get_input('wpage');
$offset     = $limit * $page;
$order      = get_input('worder');
$ownerids   = explode(',', get_input('wownerids', false));
$tags       = explode(',', get_input('wtags', false));
if(!$limit) {
    die(json_encode(export_result(400, array())));
}

/*
#define kOrderByDateASC         @"guid_asc"
#define kOrderByDateDESC        @"guid_desc"
#define kOrderByRatingASC       @"likeno_desc"
#define kOrderByRatingDESC      @"likeno_desc"
#define kOrderByLocationNearby  @"location_asc"
#define kOrderByFavorite        @"likeno_desc"
*/

$total      = 0;
$pages      = 0;
$drawings   = array();

$result = NULL;
switch($order) {
    case "guid_asc":
        if(is_array($ownerids)) {
            $total  = count_drawings_by_owner_ids($user_id, $ownerids);
            $result = get_drawings_by_owner_ids_reverse($user_id, $ownerids, $limit, $offset);
        } else {
           if($ownerids) {
               $total  = count_drawings_by_owner_id($user_id, $ownerids);
               $result = get_drawings_by_owner_id_reverse($user_id, $ownerids, $limit, $offset);
           } else {
               break;
           }
        }
        break;
    case "guid_desc":
        //echo 1;
        //print_r($ownerids);
        if(is_array($ownerids)) {
            if(count($ownerids) <= 0 || !$ownerids[0]) {
                //echo 2;
                $total = count_recent_drawings($user_id);
                $result = get_recent_drawings($user_id, $limit, $offset);
            } else {
                //echo 3;
                $total  = count_drawings_by_owner_ids($user_id, $ownerids);
                $result = get_drawings_by_owner_ids($user_id, $ownerids, $limit, $offset);
            }
        } else {
           if($ownerids) {
               //echo 4;
               $total  = count_drawings_by_owner_id($user_id, $ownerids);
               $result = get_drawings_by_owner_id($user_id, $ownerids, $limit, $offset);
           } else {
               //echo 5;
               $total = count_recent_drawings($user_id);
               $result = get_recent_drawings($user_id, $limit, $offset);
           }
        }
        break;
    case "location_asc":
        $lat        = get_input('wlat');
        $lng        = get_input('wlng');
        $distance   = 25;
        $total      = count_nearby_drawings($user_id, $lat, $lng, $distance);
        $result     = get_nearby_drawings($user_id, $lat, $lng, $distance, $limit, $offset);
        break;
    case "likeno_desc":   
    default:
        $total      = count_popular_drawings($user_id);
        $result     = get_popular_drawings($user_id, $limit, $offset);
}

$pages = intval($total / $perpage + 1);
foreach($result as $drawing_object) {
    $drawings[] = to_drawing_array($drawing_object);
}

echo 
json_encode(
        export_result(
            200, 
            array(
                'wpages' => $pages, 
                'wpage' => $page,
                'wtotal' => $total,
                'wperpage' => $perpage,
                'wtags' => $tags,
                'worder' => $order,
                'wownerids' => $ownerids,
                'wshared' => 0,
                'wdrawings' => $drawings
                )
            )
        );
?>
